package com.komodo.island.gdx.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.komodo.island.gdx.Constants;
import com.komodo.island.gdx.KomodoIsland;

public abstract class SlideScreen extends GameScreen implements InputProcessor {

	protected TextureRegion background;
	public int xpos = Constants.GAME_WIDTH / 2;
	public int ypos = Constants.GAME_HEIGHT / 2;

	private int xbpos;
	private int ybpos;

	private int xtouch;
	private int ytouch;
	private int xtouch2;
	private int ytouch2;

	private int xdown;
	private int ydown;

	private int dx;
	private int dy;

	public int MAX_PAGE = 3;
	public int MIN_PAGE = 1;
	public int PAGE_NUM = 3;

	public int page = 1;
	public boolean usePage = false;

	private boolean smooth = true;
	private boolean pageMoved = false;

	@Override
	public void init(KomodoIsland komodoIsland, Texture texture) {
		// TODO Auto-generated method stub
		super.init(komodoIsland, texture);

//		background = new TextureRegion(texture, 0, 0, 2048, 320);
		usePage = true;
	}

	@Override
	public void onScreenRun() {
		// TODO Auto-generated method stub

		if (gameState == GAME_START_ANIMATION) {
			if (!onGameStartAnimation())
				gameState = GAME_RUNNING;
		} else if (gameState == GAME_RUNNING) {
			onGameRunning();

			if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
				xpos -= 10;
			}
			if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
				xpos += 10;
			}
			if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
				ypos += 10;
			}
			if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
				ypos -= 10;
			}
			
			if (Gdx.input.isKeyPressed(Input.Keys.A)) {
				camera.zoom = 1;
			}
			if (Gdx.input.isKeyPressed(Input.Keys.Z)) {
				camera.zoom = 0.2f;
			}
			
			
			

			if (smooth) {
				if (xpos - 10 < Constants.GAME_WIDTH / 2) {
					xpos = Constants.GAME_WIDTH / 2;
				} else if (xpos + 10 > (Constants.GAME_WIDTH * MAX_PAGE)-240 ) {
					xpos = Constants.GAME_WIDTH * MAX_PAGE-240;
				}

				if (!pageMoved)
					moveSmooth();

			} else {
				if (xpos < Constants.GAME_WIDTH / 2) {
					xpos = Constants.GAME_WIDTH / 2;
				} else if (xpos > Constants.GAME_WIDTH * MAX_PAGE -240) {
					xpos = Constants.GAME_WIDTH * MAX_PAGE -240;
				}
				xpos = (page - 1) * Constants.GAME_WIDTH + Constants.GAME_WIDTH
						/ 2;
			}

			camera.position.set(xpos, ypos, 0);
			camera.update();
		}

	}

	private void moveSmooth() {
		// TODO Auto-generated method stub
		int destination = (page - 1) * Constants.GAME_WIDTH
				+ Constants.GAME_WIDTH / 2;
		if (xpos > destination) {
			// to the left
			xpos -= 10;
			if (xpos <= destination)
				pageMoved = true;
		} else {
			// to the right
			xpos += 10;
			if (xpos >= destination)
				pageMoved = true;
		}

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		Gdx.input.setInputProcessor(this);
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
		Gdx.input.setInputProcessor(komodoIsland.input);
	}
	
	

	/*
	 * Input Processor
	 */

	public boolean keyDown(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean keyTyped(char arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean keyUp(int arg0) {
		// TODO Auto-generated method stub
		if ((arg0 == com.badlogic.gdx.Input.Keys.P)
				|| (arg0 == com.badlogic.gdx.Input.Keys.MENU)
				|| (arg0 == com.badlogic.gdx.Input.Keys.BACK)) {
			if (gameState == GAME_RUNNING) {
				pauseGame();
			} else if (gameState == GAME_PAUSE) {
				resumeGame();
			}

		}

		return false;
	}

	public boolean scrolled(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		xtouch = arg0;
		xdown = xtouch;

		ytouch = arg1;
		ydown = ytouch;

		xbpos = xpos;
		ybpos = ypos;
		if (gameState == GAME_PAUSE)
			return pauseDialog.touchDown(arg0, arg1, arg2, arg3);
		else if (gameState == GAME_WIN)
			return winDialog.touchDown(arg0, arg1, arg2, arg3);
		else if (gameState == GAME_LOSE)
			return loseDialog.touchDown(arg0, arg1, arg2, arg3);

		return false;
	}

	public boolean touchDragged(int arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		xtouch2 = arg0;
		if (xtouch2 > xtouch) {
			xpos -= (xtouch2 - xtouch);
		} else {
			xpos += (xtouch - xtouch2);
		}
		xtouch = xtouch2;

		if (gameState == GAME_PAUSE)
			return pauseDialog.touchDragged(arg0, arg1, arg2);
		else if (gameState == GAME_WIN)
			return winDialog.touchDragged(arg0, arg1, arg2);
		else if (gameState == GAME_LOSE)
			return loseDialog.touchDragged(arg0, arg1, arg2);

		return false;
	}

	public boolean touchMoved(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		// System.out.println(page);

		if (usePage) {
			dx = arg0 - xdown;
			if (Math.abs(dx) > 30) {
				if (arg0 > xdown) {
					// camera move to the left
					// System.out.println("Ke kiri");
					if (page > MIN_PAGE) {
						if (validatePage()) {
							page--;

							if (smooth) {
								pageMoved = false;
							}
						}

					}

				} else {
					// camera move to the right
					// System.out.println("Ke kanan");
					if (page < MAX_PAGE) {
						if (validatePage()) {
							page++;
							if (smooth) {
								pageMoved = false;
							}
						}
					}
				}

			} else {
				xpos = xbpos;
				ypos = ybpos;

			}
		}

		if (gameState == GAME_PAUSE)
			return pauseDialog.touchUp(arg0, arg1, arg2, arg3);
		else if (gameState == GAME_WIN)
			return winDialog.touchUp(arg0, arg1, arg2, arg3);
		else if (gameState == GAME_LOSE)
			return loseDialog.touchUp(arg0, arg1, arg2, arg3);

		return false;
	}

	private boolean validatePage() {
		if ((xpos >= (page - 1) * 480 - 240) && (xpos <= (page) * 480 + 240)) {
			return true;
		}
		return false;

	}

//	@Override
//	protected boolean onGameStartAnimation() {
//		// TODO Auto-generated method stub
//		return true;
//	}
	
	

}
